package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.KetopParkingRecordDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.request.ParkingRecordRequest;
import entity.ExcelData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;
import utils.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName:KetopParkingService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/9/26 14:38
 * @Version 1.0
 */
@Service
public class KetopParkingService {

    @Value("${module-url.parkingUrl}")
    private String parkingUrl;

    /**
     * 停车缴费记录(科拓版本)
     *
     * @param requestBody
     * @return
     */
    public PageDTO<KetopParkingRecordDTO> parkingRecord(ParkingRecordRequest requestBody) {

        String json = Request.postJson(parkingUrl + "ketop_parking/parking_record", requestBody);
        PageDTO<KetopParkingRecordDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<KetopParkingRecordDTO> payNotifyDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                KetopParkingRecordDTO object = list.getObject(i, KetopParkingRecordDTO.class);
                if (object.getDisType()==null||object.getDisType()!=2){
                    object.setDisTypeString("组合支付");
                }else{
                    object.setDisTypeString("券支付");
                }
                DecimalFormat df = new DecimalFormat("0.00");
                double totalFreeFee = object.getTotalFreeFee().doubleValue();
                String totalFreeFeeYuan = df.format(totalFreeFee / 100);
                double totalFee = object.getTotalFee().doubleValue();
                String totalFeeYuan = df.format(totalFee / 100);
                object.setTotalFreeFeeYuan(totalFreeFeeYuan);
                object.setTotalFeeYuan(totalFeeYuan);
                payNotifyDTOS.add(object);
            }
            data.setList(payNotifyDTOS);
            return data;
        }
        return pageDTO;
    }


    public void exportParkingRecord(HttpServletResponse response, ParkingRecordRequest requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("车牌号");
        titles.add("用户名");
        titles.add("用户手机号");
        titles.add("用户卡号");
        titles.add("订单号");
        titles.add("总费用（单位：元）");
        titles.add("单次抵扣费用（单位：元）");
        titles.add("支付方式");
        titles.add("缴费时间");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<KetopParkingRecordDTO> pageDTO = this.parkingRecord(requestBody);
        List<KetopParkingRecordDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getPlateNum());
            row.add(vo.getNickName());
            row.add(vo.getPhone());
            row.add(vo.getOrderNo());
            row.add(vo.getTotalFeeYuan());
            row.add(vo.getTotalFreeFeeYuan());
            row.add(vo.getDisTypeString());
            row.add(vo.getPayTime());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response,"停车缴费记录" + now,data);
    }



}
